﻿using System;
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Cmd;
using GameData;
using UnityEngine.UI;

public class CustomerInfo : MonoBehaviour
{

    public GameObject PreObj;
    public GameObject Content;
    public GridLayoutGroup GridLayoutGroup;

    public GameObject signBtn;
    public GameObject noBtn;
    public GameObject allBtn;
    public GameObject expoetBtn;
    public GameObject RefashBtn;
    public bool isShowNum = false;

    public int state=0;

	// Use this for initialization
	void Start () {

        EventTriggerListener.Get(signBtn).onClick = OnSingn;
        EventTriggerListener.Get(noBtn).onClick = OnNo;
        EventTriggerListener.Get(allBtn).onClick = OnallBtn;
        EventTriggerListener.Get(expoetBtn).onClick = OnexpoetBtn;
        EventTriggerListener.Get(RefashBtn).onClick = OnRefashBtn;

        OnNumberInfo();
        state = 0;
	}
    //
    public void OnRefashBtn(GameObject obj)
    {
        GetCustomerInfo  gci = new GetCustomerInfo();
        gci.Send(new DataInfo(), OnSendRefash);

    }

    void OnSendRefash(CustomerSignDataList customerSignDataList)
    {
        Debug.Log("返回啦啦啦阿拉");
        GlobalVO.GetInstance().CustomerSignDataList = customerSignDataList;
        OnNumberInfo();
    }


    public void OnexpoetBtn(GameObject obj)
    {
        //导出
       

#if UNITY_STANDALONE_WIN || UNITY_EDITOR
        Debug.Log("window");
        string path = Application.dataPath + "/签到情况.csv";
        FileInfo fileinfo = new FileInfo(path);
        StreamWriter fileStream = fileinfo.CreateText();
        fileStream.WriteLine("客户,电话号码,是否签到");
        //
        //
        var dic = GlobalVO.GetInstance().CustomerSignDataList.csList;
        for (int i = 0; i < dic.Count; i++)
        {
            CustomerSignData p = dic[i];
            string line = p.name;
            line += ",";
            line += p.phone;
            line += ",";
            line += p.isSign;
            fileStream.WriteLine(line);
        }


        fileStream.Close();
        fileinfo = null;

#endif


    }



    public void OnallBtn(GameObject obj)
    {
        state = 0;
        for (int i = 0; i < Content.transform.childCount; i++)
        {
            Transform tf = Content.transform.GetChild(i);
            CusInfo cusinfo = tf.GetComponent<CusInfo>();
            if (cusinfo != null)
            {
                if (cusinfo.CustomerData != null )
                {
                    tf.gameObject.SetActive(true);
                }
                else
                {
                    tf.gameObject.SetActive(false);
                }

            }
        }
        Resize();

    }
    public void OnSingn(GameObject obj)
    {
        state = 1;
        for (int i = 0; i < Content.transform.childCount; i++)
        {
            Transform tf = Content.transform.GetChild(i);
            CusInfo cusinfo = tf.GetComponent<CusInfo>();
            if (cusinfo!= null)
            {
                if (cusinfo.CustomerData != null && !cusinfo.CustomerData.isSign)
                {
                    tf.gameObject.SetActive(false);
                }
                else
                {
                    tf.gameObject.SetActive(true);
                }
            }
        }
        Resize();
    }


    public void OnNo(GameObject obj)
    {
        state = 2;
        for (int i = 0; i < Content.transform.childCount; i++)
        {
            Transform tf = Content.transform.GetChild(i);
            CusInfo cusinfo = tf.GetComponent<CusInfo>();
            if (cusinfo != null)
            {
                if (cusinfo.CustomerData != null && !cusinfo.CustomerData.isSign)
                {
                    tf.gameObject.SetActive(true);
                }
                else
                {
                    tf.gameObject.SetActive(false);
                }
            }

        }
        Resize();
    }



    // Update is called once per frame
    void Update () {
	
	}

    public void OnRemoveListener()
    {

    }
    //


    void OnNumberInfo()
    {
        //
        for (int i = 0; i < Content.transform.childCount; i++)
        {
            GameObject obj = Content.transform.GetChild(i).gameObject;
            obj.SetActive(false);
            CusInfo cusinfo = obj.GetComponent<CusInfo>();
            cusinfo.CustomerData = null;
        }
        int numCount = GlobalVO.GetInstance().CustomerSignDataList.csList.Count;
        
        List<CustomerSignData> clist = GlobalVO.GetInstance().CustomerSignDataList.csList;
        int childCount = Content.transform.childCount;
        int vnck = Math.Max(childCount, numCount);

        for (int i = 0; i < vnck; i++)
        {

            if (i < numCount)
            {
                CusInfo cusinfo;
                if (i < childCount)
                {
                    Transform objtf = Content.transform.GetChild(i);
                    objtf.gameObject.SetActive(true);
                    cusinfo = objtf.GetComponent<CusInfo>();

                }
                else
                {
                    GameObject obj = GameObject.Instantiate(PreObj);
                    obj.transform.SetParent(Content.transform, false);
                    obj.transform.SetAsLastSibling();
                    cusinfo = obj.GetComponent<CusInfo>();
                    cusinfo.isShowNum = isShowNum;
                }
                //
                cusinfo.SetCustomer(clist[i]);
                if (state==1)
                {
                    if (cusinfo.CustomerData.isSign)
                    {
                        cusinfo.gameObject.SetActive(true);
                    }
                    else
                    {
                        cusinfo.gameObject.SetActive(false);

                    }
                }
                else if (state == 2)
                {
                    if (cusinfo.CustomerData.isSign)
                    {
                        cusinfo.gameObject.SetActive(false);
                    }
                    else
                    {
                        cusinfo.gameObject.SetActive(true);

                    }
                }

            }
            else
            {
                Transform objtf = Content.transform.GetChild(i);
                objtf.gameObject.SetActive(false);

            }

        }

        Resize();
    }

    public void Resize()
    {
        int num = 0;
        for (int i = 0; i < Content.transform.childCount; i++)
        {
            RectTransform rect = Content.transform.GetChild(i).GetComponent<RectTransform>();
            if (rect.gameObject.activeSelf)
            {
                num++;
            }


        }
        RectTransform contRect = Content.GetComponent<RectTransform>();
        float dy = num*(GridLayoutGroup.cellSize.y + GridLayoutGroup.spacing.y)/2;


        contRect.sizeDelta = new Vector2( contRect.rect.width,dy);

    }


}
